package gew.table;

import gew.utils.SqlUtils;
import java.util.ArrayList;
import java.util.List;

public class TableRow {

    private String id = "";
    private final ArrayList<TableCellDimension> hDimCells;
    private final ArrayList<TableCellDimension> hDimCellsReverse;
    private final ArrayList<TableCellMeasure> vDimCells;
    private final ArrayList<TableCellMeasure> vDimCellsVisible;
    private final int index;
    private byte selected;

    TableRow(int index) {
        hDimCells = new ArrayList<>();
        hDimCellsReverse = new ArrayList<>();
        vDimCells = new ArrayList<>();
        vDimCellsVisible = new ArrayList<>();
        this.index = index;
        selected = 1;
    }

    public TableRow(TableRow row, int index) {
        this(index);
        this.id = row.id;
        this.selected = row.selected;
        for (int i = 0; i < row.getHDimCells().size(); i++) {
            this.hDimCells.add(new TableCellDimension(row.getHDimCells().get(i).getDim(), true, this));
        }
        reverseCells(hDimCells, hDimCellsReverse);
    }

    void addDimensionValue(TableDimensionValue v) {
        TableCellDimension cell = new TableCellDimension(v, true, this);
        if (v.getSelected() == 1 && selected == 1)
            selected = 1;
        else
            selected = 0;
        hDimCells.add(cell);
        reverseCells(hDimCells, hDimCellsReverse);
        id = id.isEmpty() ? v.getId().toString()
                : id.concat(SqlUtils.UNDERBAR).concat(v.getId().toString());
    }

    private static void reverseCells(ArrayList<TableCellDimension> source, ArrayList<TableCellDimension> target) {
        target.clear();
        for (int i = source.size() - 1; i >= 0; i--) {
            target.add(source.get(i));
        }
    }

    public String getId() {
        return id;
    }

    public List<TableCellDimension> getHDimCells() {
        return hDimCells;
    }

    public List<TableCellDimension> getHDimCellsReverse() {
        return hDimCellsReverse;
    }

    public int getIndex() {
        return index;
    }

    String getSql() {
        String r = SqlUtils.SQL_SELECT
                .concat(SqlUtils.SPACE)
                .concat(SqlUtils.QUOTE)
                .concat(this.id)
                .concat(SqlUtils.QUOTE)
                .concat(SqlUtils.SPACE)
                .concat(SqlUtils.SQL_ALIAS_ID);
        for (int i = 0; i < hDimCells.size(); i++) {
            r = r
                    .concat(SqlUtils.COMMA)
                    .concat(hDimCells.get(i).getDim().getId().toString())
                    .concat(SqlUtils.SPACE)
                    .concat(hDimCells.get(i).getDim().getTableDimension().getFactTableField())
                    .concat(SqlUtils.COMMA)
                    .concat(hDimCells.get(i).getDim().getOrderBy().toString())
                    .concat(SqlUtils.SPACE)
                    .concat(SqlUtils.SQL_ALIAS_OB)
                    .concat(hDimCells.get(i).getDim().getTableDimension().getIndex().toString());
        }
        return r;
    }

    void setVDimCells(ArrayList<TableCellMeasure> items) {
        this.vDimCells.clear();
        this.vDimCellsVisible.clear();
        for (int i = 0; i < items.size(); i++) {
            TableCellMeasure item = items.get(i);
            if (item.getColumn().getMeasure().isShowInTable()) {
                vDimCellsVisible.add(item);
            }
            vDimCells.add(item);
        }
    }

    public void addVDimCell(String valueStr, TableColumn column) {
        TableCellMeasure cell = new TableCellMeasure(valueStr, column, this);
        if (cell.getColumn().getMeasure().isShowInTable()) {
            vDimCellsVisible.add(cell);
        }
        vDimCells.add(cell);
    }

    public Iterable<TableCellMeasure> getVDimCells() {
        return vDimCells;
    }

    public Iterable<TableCellMeasure> getVDimCellsVisible() {
        return vDimCellsVisible;
    }
    
    public byte getSelected(){
        return this.selected;
    }

}
